//
//  MovieDeteliViewController.m
//  UI_豆瓣扩展
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "MovieDeteliViewController.h"
#import "Movie.h"
#import "MovieDetailView.h"
#import "AppTools.h"
#import "UIImageView+WebCache.h"
#import "DataBaseCollect.h"
#import "FirstViewController.h"

@interface MovieDeteliViewController ()

@property (nonatomic, retain)MovieDetailView *movieDV;

@end

@implementation MovieDeteliViewController
- (void)dealloc
{
    [_movieDV release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.movieDV = [[MovieDetailView alloc]init];
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [scrollView addSubview:self.movieDV];
    [self.view addSubview:scrollView];
    [scrollView release];
    [_movieDV release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_share.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightBar)];
    [[UIImage imageNamed:@"btn_nav_share.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftBar)];
    [[UIImage imageNamed:@"btn_nav_back.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //请求数据
    NSString *urlStr = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/searchmovie.php?movieId=%@",self.movieID];
    
    [AppTools getDataWithURL:urlStr block:^(id result) {
        
        NSMutableDictionary *Dic = result;
        NSDictionary *resultDic = [Dic objectForKey:@"result"];
        self.navigationItem.title = [resultDic objectForKey:@"title"];
        self.movieDV.ratingLabel.text = [resultDic objectForKey:@"rating"];
        self.movieDV.rating_countLabel.text = [resultDic objectForKey:@"rating_count"];
        
        self.movieDV.release_dateLabel.text = [resultDic objectForKey:@"release_date"];
        
        self.movieDV.runtimeLabel.text = [resultDic objectForKey:@"runtime"];
        self.movieDV.typeLabel.text = [resultDic objectForKey:@"genres"];
        self.movieDV.countryLabel.text = [resultDic objectForKey:@"country"];
        
        NSURL *url = [NSURL URLWithString:[resultDic objectForKey:@"poster"]];
        [self.movieDV.headImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"picholder.png"]];
        
        self.movieDV.actorsLabel.text = [resultDic objectForKey:@"actors"];
        [self.movieDV.actorsLabel sizeToFit];
        
        self.movieDV.plot_simpleLabel.text = [resultDic objectForKey:@"plot_simple"];
        [self.movieDV.plot_simpleLabel sizeToFit];
        
        //设置scrollView滚动范围
        CGFloat height = [[self class]heightForContent:[resultDic objectForKey:@"plot_simple"]];
        CGRect tempFrame = self.movieDV.plot_simpleLabel.frame;
        tempFrame.size.height = height;
        self.movieDV.plot_simpleLabel.frame = tempFrame;
        
        scrollView.contentSize = CGSizeMake(375, tempFrame.size.height + 450);
        
    }];
    
}

//收藏按钮
- (void)rightBar
{
    if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"isLogin"] isEqualToString:@"1"]) {
        [[DataBaseCollect shareDataBase]openDB];
        [[DataBaseCollect shareDataBase]createDB];
        
        NSString *name = [[NSUserDefaults standardUserDefaults]stringForKey:@"firstName"];
        NSMutableArray *array = [[DataBaseCollect shareDataBase]selectMovieCollect:name MovieName:self.navigationItem.title];
        if (array.count == 0) {
            
            [[DataBaseCollect shareDataBase]insertMovie:self.movieID name:self.navigationItem.title userName:name];
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"取消");
            }];
            UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"OK");
            }];
            [alert addAction:canact];
            [alert addAction:othact];
            [self presentViewController:alert animated:YES completion:nil];
        }else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"已收藏" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"取消");
            }];
            UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"OK");
            }];
            [alert addAction:canact];
            [alert addAction:othact];
            [self presentViewController:alert animated:YES completion:nil];
        }
    }else{
        FirstViewController *firstVC = [[FirstViewController alloc]init];
        UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:firstVC];
        [self presentViewController:navC animated:YES completion:nil];
        [firstVC release];
        
        
        //跳转后隐藏tabbar
//        loginVC.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:loginVC animated:YES];

    }
}

+ (CGFloat)heightForContent:(NSString *)content{
    //参数1.设置计算高度按照什么样的size来计算
    //注意:宽度一定于label宽度一致,高度尽量大一些
    //参数2.设置计算类型
    //参数3.设置计算内容按照多大的字体计算
    //注意:字体大小也要跟label一样
    // NSFontAttributeName
    //参数4.系统备用参数,设置为nil
    CGSize size = CGSizeMake(335, 10000);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17.0],NSFontAttributeName, nil];
    CGRect frame = [content boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return frame.size.height;
}

- (void)leftBar
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
